package com.beijiteshop.shop.ui.notification.vm

import androidx.databinding.ObservableBoolean
import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.network.ext.*
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.ShopApplication
import com.beijiteshop.shop.model.api.response.NotificationCountBean
import com.beijiteshop.shop.model.api.response.NotificationDetailEntity
import com.beijiteshop.shop.model.api.response.NotificationDetailListBean
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.notification.repo.NotificationRepo

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/16 11:09
 * @Description
 */
class NotificationVM(private val repo: NotificationRepo) : BaseViewModel(){

    val liveDetails = MutableLiveData<MutableList<NotificationDetailEntity>>()

    val liveNotificationCount = MutableLiveData<NotificationCountBean>()

    /**
     * 已读状态
     * String参数代表messageId
     */
    val liveReadState = MutableLiveData<String>()

    /**
     * 全部已读状态
     * String参数代表成功/失败信息
     */
    val liveAllRead = MutableLiveData<String>()

    fun getNotificationCount() = launch {
        repo.getNotificationCount()
            .serverData()
            .onSuccess {
                onBizOK { _, data, _ ->
                    liveNotificationCount.postValue(data)
                }

                onBizError { code, message ->
                    //如果接口报错就不管了
                }
            }
            .onFailure {
                //不管了
            }
    }

    fun getNotificationList(page: Int) = launch {
        repo.getNotificationList(page)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    if (data.isNullOrEmpty()){
                        liveEmpty.postValue(null)
                    }else{
                        liveDetails.postValue(transferNotificationList(data))
                    }
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }

                onBizEmpty {
                    liveEmpty.postValue(it)
                }

            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    private fun transferNotificationList(data: MutableList<NotificationDetailListBean>): MutableList<NotificationDetailEntity>{
        val result = mutableListOf<NotificationDetailEntity>()
        data.forEach {
            //添加日期
            result.add(NotificationDetailEntity(NotificationDetailEntity.TYPE_DATE,it.date,"",
                ObservableBoolean(false),"","",null))
            //添加消息实体
            it.message.forEach { message ->
                result.add(NotificationDetailEntity(
                    NotificationDetailEntity.TYPE_ITEM,
                    it.date,
                    getMessageName(message.bus_OrdersID),
                    ObservableBoolean(message.isRead),
                    message.createTime,
                    message.message,
                    message))
            }
        }
        return result
    }

    private fun getMessageName(orderId: String?): String{
        if (orderId.isNullOrEmpty()){
            return ShopApplication.shopApplication.getContext().resources.getString(R.string.notification_message)
        }else{
            return ShopApplication.shopApplication.getContext().resources.getString(R.string.order_message)
        }
    }

    fun setReadState(isOrder: Boolean,messageId: String) = launch {
        if (isOrder){
            repo.setOrderReadState(messageId)
                .serverData()
                .onSuccess {
                    onBizOk { msg, data ->
                        liveReadState.postValue(messageId)
                        notifyUpdateMainCount(false)
                    }

                }
        }else{
            repo.setCouponReadState(messageId)
                .serverData()
                .onSuccess {
                    onBizOk { msg, data ->
                        liveReadState.postValue(messageId)
                    }
                }

        }
    }

    fun setAllRead() = launch {
        repo.setAllRead()
            .serverData()
            .onSuccess {
                onBizOk { msg, _ ->
                    var message = msg
                    if (message.isNullOrEmpty()){
                        message = ShopApplication.shopApplication.getContext().resources.getString(R.string.operation_successful)
                    }
                    liveAllRead.postValue(message)
                    notifyUpdateMainCount(true)
                }

                onBizError { _, msg ->
                    var message = msg
                    if (message.isNullOrEmpty()){
                        message = ShopApplication.shopApplication.getContext().resources.getString(R.string.operation_failed)
                    }
                    liveAllRead.postValue(message)
                }
            }
            .onFailure {
                liveAllRead.postValue(it.message)
            }
    }

    //通知首页消息列表数量更新
    private fun notifyUpdateMainCount(isReadAll: Boolean){
        EventUtil.post(EventKey.DECREASE_MSG_COUNT,isReadAll)
    }
}